<template>
  <div>
    <el-dialog
      width="35%"
      :title="info.isAdd?'添加角色':'修改角色'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="user" label-width="80px">
        <el-form-item label="角色名称">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="treedata"
            show-checkbox
            node-key="id"
            :props="{children: 'children',label: 'title'}"
            ref="tree"
          ></el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqroleadd,
  reqmenulist,
  reqroleinfo,
  reqroleedit
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";
export default {
  props: ["info", "data"],
  mixins: [formClose],
  data() {
    return {
      user: {
        rolename: "",
        menus: [],
        status: 1
      },
      isshow: true,
      isAdd: true,
      treedata: []
    };
  },
  computed: {
    ...mapGetters({
      uinfo: "info"
    })
  },
  mounted() {
    reqmenulist({ istree: true }).then(res => {
      if (res.data.code === 200) {
        this.treedata = res.data.list;
      }
    });
  },
  methods: {
    ...mapActions({
      changeInfo: "changeInfo"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.rolename == "") {
          errorAlert("角色名称不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqroleadd(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    },
    getOne(id) {
      reqroleinfo({ id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.$refs.tree.setCheckedKeys(JSON.parse(this.user.menus));
          this.user.id = id;
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
        reqroleedit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            if (this.user.id === this.uinfo.roleid) {
              this.changeInfo({});
              this.$router.replace("/login");
              return;
            }
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    },
    empty() {
      (this.user = {
        rolename: "",
        menus: [],
        status: 1
      }),
        this.$refs.tree.setCheckedKeys([]);
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
</style>